<?php
/**
 * Ora - 后台 主题管理模块
 * @author lan7
 * @copyright lan7.net
 * @version 2.0
 */

if(!defined('IN_ORA')) {
	exit('Access Denied');
}
class admin_tpl extends mod {
	function admin_tpl(){
		parent::__construct();
		session_start();
		$this->setTplDir(PUBLIC_PATH);
		$this->setMsgTpl("admin/msg.html");
		if(!isset($_SESSION[$GLOBALS['ora_cfg']['sess_prefix']."admin"])){
			$this->showMsg("请先登录!",parse_uri("common-login"),1);
		}
		import("lib.tpl");
	}

	/**
	 * 模板中心首页
	 *
	 */
	function index(){
		$myTpl = array();
		$q = $this->db->query('select * from '.tn('theme').' order by Id desc');
		while ($row = $this->db->fetch_array($q)) {
			$myTpl[$row['theme_code']] = $row;
		}
		$this->assign('myTpl',$myTpl);
		$this->assign("curTpl",C("themes"));
		$this->assign('tplKeys',array_keys($myTpl));
		$this->assign('tpls',tpl_lib::getAllTpl());
		$this->display("admin/theme.html");
	}

	/**
	 * 设置默认模板 
	 *
	 */
	function enable(){
		$key = base64_decode($GLOBALS['_param']["k"]);
		$msg = "";
		if($key){
			if(is_dir(THEMES_PATH.$key)){
				import("lib.config");
				config_lib::update('/\'themes\'\s*=\>\s*?\'.*?\'/i',"'themes' => '".preg_quote($key)."'");
				$msg = "操作成功!";
			}else{
				$msg = "模板不存在!";
			}
		}
		if(isset($GLOBALS['_param']['isAjax'])){
			echo($msg == "操作成功!"?"true":"false");
		}else{
			$this->showMsg($msg,$_SERVER["HTTP_REFERER"],1);
		}
	}

	/**
	 * 模板安装
	 *
	 */
	function install(){
		$key = base64_decode($GLOBALS['_param']["key"]);
		if($key){
			import("lib.update");
			//* URL 需要动态构建 , 同时处理 服务器返回结果
			//todo
			$fileSize = update_lib::getRemoteFileSize(ORA_UPDATE."update/getTpl/key_".base64_encode($key));
			if($fileSize){
				$this->assign("fileSize",$fileSize);
				$this->assign('tpls',tpl_lib::getAllTpl());
				$this->assign("key",$key);
				$this->display("admin/theme.html");
			}
		}
		$this->showMsg("获取模板失败!",parse_uri('admin_tpl-index'),1);
	}

	/**
	 * 安装模板 *
	 *
	 */
	function doInstall(){
		$key = base64_decode($GLOBALS['_param']["key"]);
		if($key){
			import("lib.update");
			import("lib.tpl");
			$f = update_lib::getFile(ORA_UPDATE."update/getTpl/key_".base64_encode($key),PLUGIN_PATH."tpl");
			if(unpackZip(THEMES_PATH.$key."/",$f)){
				if(!tpl_lib::insertTpl(THEMES_PATH.$key."/desc.xml")){
					import("lib.tpl");
					tpl_lib::delTpl($key);
					exit("fail");
				}
			}
		}
	}

	/**
	 * 删除进度条临时文件
	 *
	 */
	function delNow(){
		//io::remove(UPLOAD_PATH."/now");
		io::write(UPLOAD_PATH."/now","0");
	}



	/**
	 * 新增模板
	 *
	 */
	function add(){
		$this->display("admin/theme.html");
	}

	/**
	 * 本地模板删除
	 *
	 */
	function del(){
		$id = intval($GLOBALS['_param']["id"]);
		$result = $this->db->getOne("select * from ".tn('theme')." where Id = $id ");
		if($result){
			import("lib.tpl");
			tpl_lib::delTpl($result['theme_code']);
			$this->db->execute("delete from ".tn('theme')." where Id = $id ");
			$this->showMsg("删除成功!",parse_uri("admin_tpl-index"),1);
		}
		$this->showMsg("无效的模板ID!");
	}

	/**
	 * 本地模板管理
	 *
	 */
	function mgr(){
		$this->assign("tpls",$GLOBALS['db']->getAll("select * from ".tn('theme')." order by Id desc"));
		$this->assign("curTpl",C("themes"));
		$this->display("admin/theme.html");
	}
	/**
	 * 上传模板
	 *
	 */
	function upload(){
		$dataFilePath = "";
		import("lib.upload");
		$up = new Upload("zip");
		if(!$up->errInfo){
			$fileName = $up->files[0]["name"];
			$fileName = substr($fileName,0,strlen($fileName)-4);
			//重命名为 与 theme_code 一致
			@rename(ROOT_PATH."upload/".$up->files[0]["urlPath"],ROOT_PATH."upload/zip/tpl_".$fileName.".zip");
			$dataFilePath = "upload/zip/tpl_".$fileName.".zip";
			$fileDir = ROOT_PATH."themes/".$fileName.'/';
			if(unpackZip($fileDir,ROOT_PATH.$dataFilePath)){
				if(is_file($fileDir."desc.xml")){
					$strBuf = file_get_contents($fileDir."desc.xml");
					if(sizeof($strBuf)>0){
						$xobj = simplexml_load_string($strBuf);
						if($xobj){
							$sql = "insert into ".tn('theme')."(theme_name,theme_pic,theme_author,theme_homepage,theme_file ";
							$sql .=",theme_date,theme_keywords,theme_color,theme_type,theme_style,theme_code,ver) ";
							$sql.=" values('{$xobj->theme_name }','{$xobj->theme_pic }','{$xobj->theme_author }',";
							$sql.="'{$xobj->theme_homepage}','{$dataFilePath}',".time().",'{$xobj->theme_keywords }',";
							$sql.="'{$xobj->theme_color }',0,'{$xobj->theme_style }','{$xobj->theme_code }','{$xobj->theme_ver}')";
							if($this->db->execute($sql)){
								$this->showMsg("导入成功!",parse_uri('admin_tpl-index|true'),1);
							}
						}
					}
				}
			}
			io::remove(ROOT_PATH.$dataFilePath);
			io::removeDir($fileDir);
			$this->showMsg("导入失败!",$_SERVER["HTTP_REFERER"],1);

		}else{
			$this->showMsg("数据上传失败! ".$up->errInfo[0],$_SERVER["HTTP_REFERER"],1);
		}

		$this->display("admin/theme.html");
	}
}
?>